﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StoreIModels;
using StoreISet.Applicationes.Command;
using StoreRepositorys.Interface;
using System.Reflection.Metadata.Ecma335;

namespace StoreISet.Controllers
{
    /// <summary>
    /// 门店
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StoreController : ControllerBase
    {
      public IMediator _mediator;
        public readonly IStoreRepository _storeRepository;
        public StoreController(IMediator mediator, IStoreRepository storeRepository)
        {
            _mediator = mediator;
            _storeRepository = storeRepository;
        }
        /// <summary> 
        /// 前端门店显示启用的
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> FrontStrore(int sid)
        {
            var res = (from x in await _storeRepository.GetAllasync()
                       where (x.StoreState == "启用" && (sid == 0 || x.StoreId == sid))
                       select x).ToList();
            return Ok(res);

        }
        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create(StoreCreateCommand models)
        {
            try
            {
                var res = await _mediator.Send(models);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
          
        }
        /// <summary>
        /// 门店反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Fand(StoreFandCommand command)
        {
            var rs= await _mediator.Send(command);
           return Ok(rs);
        }
        /// <summary>
        /// 门店修改
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update(StoreUpdateCommand models)
        {
            var res = await _mediator.Send(models);
            return Ok(res);
        }
        /// <summary>
        /// 停用门店
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> State(StorelStateCommand command)
        {

            var res=await _mediator.Send(command);
            return Ok(res);
        }
    }
}
